<?php
include_once('../../php/postgre.php');
$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];
if($act=='save'){
	$id_skpd = $_POST['id_skpd'];
	$id_bidang = $_POST['id_bidang'];
	$nama_skpd = $_POST['text-nama-skpd'];
	$username = $_POST['text-username'];
	$password = $_POST['text-password'];
	$level = $_POST['level'];
	
	if(!is_numeric($id_skpd)){
		echo "Err. invalid id.";
		exit;
	}
	
	if($id_skpd > 0){
		exec_query("update spm_skpd set id_bidang ='".$id_bidang."', nama_skpd = '".$nama_skpd."', passwordx = '".$password."', level = '".$level."' WHERE id_skpd = ".$id_skpd);
		echo 'success__';
		//$id_skpd = 0;
	}else{
		$maxid = get_maxid('id_skpd','spm_skpd');
		exec_query("insert into spm_skpd(id_skpd, id_bidang, nama_skpd, userx, passwordx, level) values ('".$maxid."','".$id_bidang."','".$nama_skpd."', '".$username."', '".$password."', '".$level."')");
		$stored = get_data('select id_skpd from spm_skpd where id_skpd='.$maxid);
		if($stored['id_skpd']==$maxid){
			echo 'success__';
			$id_skpd = $maxid;
		}
	}
	
	echo $id_skpd.'__';
	view_program();
}else if($act == 'delete'){
	$id_skpd = $_POST['id_skpd'];
	if(is_numeric($id_skpd)){
		exec_query("DELETE FROM spm_skpd WHERE id_skpd =".$id_skpd);
		echo 'success__';
	}else{
		echo 'Error';
	}
	
	echo $id_skpd.'__';
	view_program();
}else if($act=='edit'){
	$id_skpd = $_POST['id_skpd'];
	if(is_numeric($id_skpd)){
		$data = get_data("select id_skpd, id_bidang, nama_skpd, userx, passwordx from spm_skpd where id_skpd=".$id_skpd);
		$ress = implode($data,'__');
		print_r($ress);
	}else{
		echo $id_skpd;
		echo 'error';
	}
}

function view_program(){
	$no = 1;
	$data = get_datas("SELECT s.id_skpd, s.id_bidang, s.nama_skpd, s.userx, s.level, b.nama_bidang FROM spm_skpd s, spm_bidang_urusan b WHERE s.id_bidang = b.id_bidang");
	foreach($data as $data){
	?>
	<tr>
		<td style="text-align:center;"><?php echo $no?></td>
		<td><?php echo $data['nama_bidang'] ?></td>
		<td><?php echo $data['nama_skpd'] ?></td>
		<td><?php echo $data['userx'] ?></td>
		<td style="width:200px; text-align:center;">	<a class="btn btn-info btn-edit" name="<?php echo $data['id_skpd']?>"><i class="icon-edit icon-white"></i>&nbsp; Edit</a>
				<a class="btn btn-danger btn-delete" name="<?php echo $data['id_skpd']?>"><i class="icon-trash icon-white"></i>&nbsp; Delete</a>
		</td>
	</tr>
	<?php
	$no++;
	}
}
	
?>